/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: StaffController.java 47 2012-09-20 06:31:35Z zhuxiaopeng $
 * $Revision: 47 $
 * $Date: 2012-09-20 14:31:35 +0800 (四, 2012-09-20) $
 * $Author: zhuxiaopeng $
 */
package daily.web.staff_info;

import static zhu.framework.web.WebUtility.*;

import javax.ejb.*;
import javax.enterprise.context.*;
import javax.inject.*;

import zhu.framework.service.*;
import zhu.framework.web.*;
import zhu.framework.web.jsf.*;
import daily.entity.*;
import daily.service.*;
import daily.web.SessionBean;

/**
 * <p>
 * </p>
 * 
 * @author $Author: zhuxiaopeng $
 * @version $Revision: 47 $ - $Date: 2012-09-20 14:31:35 +0800 (四, 2012-09-20) $
 */
@Named
@RequestScoped
public class StaffInfoController extends ConversationalController {
	private enum ActionOutCome {
		RESULT
	}

	public String saveStaff() {
		try {
			final Staff saveStaff = staffService.saveStaff(sessionBean.getLoginStaff());
			sessionBean.setLoginStaff(saveStaff);
		} catch (final BusinessException e) {
			getWebRequestContext().addMessages(WebMessage.valueOf(e.getMessages()));
			return null;
		}
		return ActionOutCome.RESULT.toString();
	}

	public String changePassword() {
		if (password.equals(sessionBean.getLoginStaff().getPassword()) == false) {
			getWebRequestContext().addMessage(new WebMessage("現パスワードが正しくない。"));
			return null;
		}
		if (newPassword.equals(confirmPassword) == false) {
			getWebRequestContext().addMessage(new WebMessage("新パスワードと確認パスワードが違います。"));
			return null;
		}
		sessionBean.getLoginStaff().setPassword(newPassword);
		final Staff saveStaff = staffService.saveStaff(sessionBean.getLoginStaff());
		sessionBean.setLoginStaff(saveStaff);
		return ActionOutCome.RESULT.toString();
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(final String password) {
		this.password = password;
	}

	/**
	 * @return the newPassword
	 */
	public String getNewPassword() {
		return newPassword;
	}

	/**
	 * @param newPassword
	 *            the newPassword to set
	 */
	public void setNewPassword(final String newPassword) {
		this.newPassword = newPassword;
	}

	/**
	 * @return the confirmPassword
	 */
	public String getConfirmPassword() {
		return confirmPassword;
	}

	/**
	 * @param confirmPassword
	 *            the confirmPassword to set
	 */
	public void setConfirmPassword(final String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	private String password;
	private String newPassword;
	private String confirmPassword;
	@Inject
	private SessionBean sessionBean;
	@EJB
	private StaffServiceImpl staffService;
}
